package com.sean.gulimall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sean.common.utils.PageUtils;
import com.sean.common.utils.R;
import com.sean.gulimall.product.entity.BrandEntity;
import com.sean.gulimall.product.entity.CategoryBrandRelationEntity;
import com.sean.gulimall.product.entity.CategoryEntity;
import com.sean.gulimall.product.service.BrandService;
import com.sean.gulimall.product.service.CategoryBrandRelationService;
import com.sean.gulimall.product.service.CategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 品牌分类关联
 *
 * @author sean
 * @email sean_xin@126.com
 * @date 2022-05-22 21:41:06
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 列表
     */
    @RequestMapping("/list/{brandId}")
    @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long brandId){
        PageUtils page = categoryBrandRelationService.queryPage(params,brandId);

        return R.ok().put("page", page);
    }

    /**
     * 根据产品分类id获取所有品牌分类
     * /product/categorybrandrelation/brands/list
     */
    @GetMapping("/brands/list")
    public R brandList(@RequestParam(value="catId") Long catId)
    {
        if( Objects.isNull(catId))
        {
            return R.error("商品分类Id不能为空");
        }
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                .select(CategoryBrandRelationEntity::getBrandId, CategoryBrandRelationEntity::getBrandName)
                .eq(CategoryBrandRelationEntity::getCatelogId, catId));

        return R.ok().put("data",list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        if(categoryBrandRelation == null)
        {
            return R.error("参数异常");
        }
        //获取品牌名字和属性分组名字
        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
